<template>
  <div id="app">
    <!-- 卡片组件 -->
    <el-card class='login-card'>
      <!-- 登录表单 -->
      <el-form style="margin-top: 50px" :model="ruleForm" :rules="rules" ref="loginForm">
        <el-form-item  prop="iphone">
          <el-input placeholder="请输入手机号" v-model="ruleForm.iphone"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="loginFn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'App',
  data () {
    const validateIphone = (rule, value, callback) => {
      if (value[2] !== '9') {
        callback(new Error('请输入第三位是9的手机号'))
      } else {
        callback()
      }
    }

    return {
      ruleForm: {
        iphone: '',
        password: ''
      },
      rules: {
        iphone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /1[3-9]\d{9}/, message: '请输入正确格式手机号', trigger: 'blur' },
          { validator: validateIphone, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginFn () {
      this.$refs.loginForm.validate(valid => {
        // if (valid) {
        //   console.log('成功')
        // } else {
        //   console.log('失败')
        //   return false
        // }

        if (!valid) {
          console.log('失败')
          return false
        }

        // 一大段成功后要执行的代码
        console.log('成功就要发请求 跳转页面 等待一大堆操作')
      })
    }
  }
}
</script>

<style>
html,body {
  margin: 0;
}
#app {
  width: 100%;
  height: 100vh;
  background-color: pink;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-card {
  width: 440px;
  height: 300px;
}
</style>
